/*
 * Copyright (c) 2012 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.coms514.scheduleme;

import com.google.api.services.calendar.model.Event;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class TodayEventModel {

	private final Map<String, TodayEventInfo> todayEvents = new HashMap<String, TodayEventInfo>();

	int size() {
		synchronized (todayEvents) {
			return todayEvents.size();
		}
	}

	void remove(String id) {
		synchronized (todayEvents) {
			todayEvents.remove(id);
		}
	}

	TodayEventInfo get(String id) {
		synchronized (todayEvents) {
			return todayEvents.get(id);
		}
	}

	void add(Event eventToAdd) {
		synchronized (todayEvents) {
			TodayEventInfo found = get(eventToAdd.getId());
			if (found == null) {
				todayEvents.put(eventToAdd.getId(), new TodayEventInfo(eventToAdd));
			} else {
				found.update(eventToAdd);
			}
		}
	}

	public TodayEventInfo[] toSortedArray() {
		synchronized (todayEvents) {
			List<TodayEventInfo> result = new ArrayList<TodayEventInfo>();
			for (TodayEventInfo todayEvent : todayEvents.values()) {
				result.add(todayEvent.clone());
			}
			Collections.sort(result);
			return result.toArray(new TodayEventInfo[0]);
		}
	}
	public ArrayList<TodayEventInfo> toArrayList() {
		synchronized (todayEvents) {
			ArrayList<TodayEventInfo> result = new ArrayList<TodayEventInfo>();
			for (TodayEventInfo todayEvent : todayEvents.values()) {
				result.add(todayEvent.clone());
			}
			return result;
		}
	}
}
